package com.example.weather_app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.weather_app.city_manager.CityManagerActivity;
import com.example.weather_app.db.DBManager;
import com.example.weather_app.myadapter.MainViewPageAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager mainVp;
    private ImageView imgBtnAdd;
    private ImageView imgBtnMore;
    private LinearLayout mainLayoutPoint;
    private RelativeLayout mainRl;

    private List<String> citys = DBManager.queryAllCityName();
    //    private List<String> citys = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<ImageView> imageViewList = new ArrayList<>();

    private MainViewPageAdapter pageAdapter;

    //获取首选项
    private SharedPreferences bg_pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainVp = (ViewPager) findViewById(R.id.main_vp);
        imgBtnAdd = (ImageView) findViewById(R.id.img_btn_add);
        imgBtnMore = (ImageView) findViewById(R.id.img_btn_more);
        mainLayoutPoint = (LinearLayout) findViewById(R.id.main_layout_point);
        mainRl = findViewById(R.id.main_rl);
        bg_pref = getSharedPreferences("bg_pref", MODE_PRIVATE);

        //更改背景图
        exchangeBg();

        //添加监听事件
        imgBtnAdd.setOnClickListener(this);
        imgBtnMore.setOnClickListener(this);

//        接收页面跳转的数据
        Intent intent = getIntent();
        String city = intent.getStringExtra("city");
        if (!citys.contains(city) && city != null) {
            citys.add(city);
        }


        if (citys.size() == 0) {
            citys.add("温州");
        }
        //数据初始化
        initPage();

        //加载小圆点
        pointLoad();

        //设置适配器
        pageAdapter = new MainViewPageAdapter(getSupportFragmentManager(), fragmentList);
        mainVp.setAdapter(pageAdapter);
        mainVp.setCurrentItem(fragmentList.size() - 1);
        //设置监听
        setPagerLitener();
    }

    private void exchangeBg() {
        int bg = bg_pref.getInt("bg", 0);
        switch (bg){
            case 0:
                mainRl.setBackgroundResource(R.mipmap.bg);
                break;
            case 1:
                mainRl.setBackgroundResource(R.mipmap.bg2);
                break;
            case 2:
                mainRl.setBackgroundResource(R.mipmap.bg3);
                break;
        }
    }

    private void setPagerLitener() {
        mainVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imageViewList.size(); i++) {
                    imageViewList.get(i).setBackgroundResource(R.mipmap.a1);
                }
                imageViewList.get(position).setBackgroundResource(R.mipmap.a2);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void pointLoad() {
        for (int i = 0; i < fragmentList.size(); i++) {
            ImageView iv = new ImageView(this);
            iv.setBackgroundResource(R.mipmap.a1);
            iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
            layoutParams.setMargins(0, 0, 20, 0);
            imageViewList.add(iv);
            mainLayoutPoint.addView(iv);
        }
        imageViewList.get(imageViewList.size() - 1).setBackgroundResource(R.mipmap.a2);
    }

    private void initPage() {
        for (int i = 0; i < citys.size(); i++) {
            CityFragment cityFragment = new CityFragment();
            Bundle bundle = new Bundle();
            bundle.putString("city", citys.get(i));
            cityFragment.setArguments(bundle);
            fragmentList.add(cityFragment);
        }
    }

    @Override
    public void onClick(View view) {
        Intent it = null;
        switch (view.getId()) {
            case R.id.img_btn_add:
                it = new Intent(MainActivity.this, CityManagerActivity.class);
                break;
            case R.id.img_btn_more:
                it = new Intent(MainActivity.this, MoreActivity.class);
                break;
        }
        startActivity(it);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        List<String> strings = DBManager.queryAllCityName();
        if (strings.size() == 0) {
            strings.add("温州");
        }
        //清空原始数据
        citys.clear();
        citys.addAll(strings);
        //碎片更新
        fragmentList.clear();
        initPage();
        //适配器更改
        pageAdapter.notifyDataSetChanged();
        //指示器更改
        imageViewList.clear();
        mainLayoutPoint.removeAllViews();
        pointLoad();
        mainVp.setCurrentItem(fragmentList.size() - 1);
    }
}